package bjut.core.channel;

import io.netty.channel.Channel;

import java.net.InetSocketAddress;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: zhuzhiming
 * @Date: 2022/5/22 16:43
 */
public class ChannelRecord {

    private static final ConcurrentHashMap<String, Channel> CHANNEL_MAP = new ConcurrentHashMap<>();


    public void putChannel(InetSocketAddress inetSocketAddress, Channel channel) {
        CHANNEL_MAP.put(inetSocketAddress.toString(), channel);
    }

    public Channel getChannel(InetSocketAddress inetSocketAddress) {

        String key = inetSocketAddress.toString();
        if (CHANNEL_MAP.containsKey(key)) {
            Channel channel = CHANNEL_MAP.get(key);
            if (channel != null && channel.isActive()) {
                return channel;
            } else {
                CHANNEL_MAP.remove(key);
            }
        }
        return null;
    }

}
